#!/usr/bin/env python3
"""
创建缺失的数据库表
"""

import sys
import os

# 添加当前目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

try:
    from app import create_app
    from extensions import db
    from models import UserStats, ConversationFile
    
    print("正在创建Flask应用...")
    app = create_app()
    
    print("正在设置应用上下文...")
    with app.app_context():
        print("正在创建数据库表...")
        db.create_all()
        print("数据库表创建完成！")
        
        # 验证表是否创建成功
        from sqlalchemy import inspect
        inspector = inspect(db.engine)
        tables = inspector.get_table_names()
        
        print(f"当前数据库中的表: {tables}")
        
        if 'user_stats' in tables:
            print("✓ user_stats 表创建成功")
        else:
            print("✗ user_stats 表创建失败")
            
        if 'conversation_files' in tables:
            print("✓ conversation_files 表创建成功")
        else:
            print("✗ conversation_files 表创建失败")
            
except Exception as e:
    print(f"创建数据库表时出错: {str(e)}")
    import traceback
    traceback.print_exc()
    sys.exit(1)